#region Using References

using System;
using System.Collections;
using System.Linq;
using Beaker.OpenCube.Mapping;

#endregion

namespace Beaker.OpenCube.Game.Converters
{
	public sealed class Coordinates3Converter<T>
		: IValueConverter
		where T : struct, IEquatable<T>
	{
		public object FromNbtToLocal( object nbtValue )
		{
			var nbtValues = nbtValue as IEnumerable;
			if( nbtValues != null )
			{
				T[] values = nbtValues.OfType<T>().ToArray();
				if( values.Length == 3 )
				{
					return new Coordinates3<T>( values[0], values[1], values[2] );
				}
			}

			throw new InvalidOperationException( "Can only convert 3 T's to Coordinates3 of T" );
		}

		public object FromLocalToNbt( object localValue, Type targetType )
		{
			if( localValue is Coordinates3<T> )
			{
				var coordinates = (Coordinates3<T>)localValue;
				return new[] { coordinates.X, coordinates.Y, coordinates.Z };
			}

			throw new InvalidOperationException( "Can only convert Coordinates3 of T to 3 T's" );
		}
	}
}